/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.jf.cloud.multishop.controller.multishop;

import com.jf.cloud.api.auth.constant.SocialType;
import com.jf.cloud.api.auth.feign.AuthSocialFeignClient;
import com.jf.cloud.api.auth.vo.AuthSocialVO;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.multishop.model.ShopMsgUser;
import com.jf.cloud.multishop.service.ShopMsgUserService;
import com.jf.cloud.multishop.vo.ShopMsgUserVO;
import com.jf.cloud.multishop.dto.ShopMsgUserDTO;

import com.jf.cloud.api.auth.bo.UserInfoInTokenBO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 
 *
 * @author zz
 * @date 2023-03-26 15:42:57
 */
@RestController("multishopShopMsgUserController")
@RequestMapping("/m/shop_msg_user")
@Tag(name = "")
public class ShopMsgUserController {

    @Autowired
    private ShopMsgUserService shopMsgUserService;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private AuthSocialFeignClient authSocialFeignClient;

    @GetMapping("/page")
    @Operation(summary = "获取列表", description = "分页获取列表")
    public ServerResponseEntity<PageVO<ShopMsgUser>> page(@Valid PageDTO pageDTO, @ParameterObject ShopMsgUserDTO shopMsgUserDTO) {
        Long shopId=AuthUserContext.get().getTenantId();
        if (Objects.equals(shopId, Constant.DEFAULT_SHOP_ID)) {
            throw new LuckException("店铺不存在,无法查询");
        }
        shopMsgUserDTO.setShopId(shopId);
        PageVO<ShopMsgUser> shopMsgUserPage = shopMsgUserService.page(pageDTO,shopMsgUserDTO);
        return ServerResponseEntity.success(shopMsgUserPage);
    }

    @GetMapping
    @Operation(summary = "获取", description = "根据id获取")
    public ServerResponseEntity<ShopMsgUser> getById(@RequestParam Long id) {
        return ServerResponseEntity.success(shopMsgUserService.getById(id));
    }

    @PostMapping
    @Operation(summary = "保存", description = "保存")
    public ServerResponseEntity<Void> save(@Valid @RequestBody ShopMsgUserDTO shopMsgUserDTO) {
        Long shopId=AuthUserContext.get().getTenantId();
        if (Objects.equals(shopId, Constant.DEFAULT_SHOP_ID)) {
            throw new LuckException("店铺不存在,无法查询");
        }
        shopMsgUserDTO.setShopId(shopId);
        if(!Objects.isNull(shopMsgUserService.getByOpenId(shopMsgUserDTO.getOpenId(),shopId))){
            throw new LuckException("请不要重复添加!");
        }
        ShopMsgUser shopMsgUser = mapperFacade.map(shopMsgUserDTO, ShopMsgUser.class);
        shopMsgUser.setId(null);
        shopMsgUserService.save(shopMsgUser);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新", description = "更新")
    public ServerResponseEntity<Void> update(@Valid @RequestBody ShopMsgUserDTO shopMsgUserDTO) {
        Long shopId=AuthUserContext.get().getTenantId();
        if (Objects.equals(shopId, Constant.DEFAULT_SHOP_ID)) {
            throw new LuckException("店铺不存在,无法查询");
        }
        shopMsgUserDTO.setShopId(shopId);
        ShopMsgUser shopMsgUser = mapperFacade.map(shopMsgUserDTO, ShopMsgUser.class);
        shopMsgUserService.update(shopMsgUser);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除", description = "根据id删除")
    public ServerResponseEntity<Void> delete(@RequestParam Long id) {
        shopMsgUserService.deleteById(id);
        return ServerResponseEntity.success();
    }

    @GetMapping("/getMpInfo")
    @Operation(summary = "根号手机号搜索微信信息", description = "根号手机号搜索微信信息")
    public ServerResponseEntity<AuthSocialVO> getMpInfo(@RequestParam String phone) {
        AuthSocialVO authSocialVO = authSocialFeignClient.getBizUserIdByPhone(phone).getData();
        if(Objects.isNull(authSocialVO)){
            throw new LuckException(phone+"手机号还未绑定微信公众号信息!");
        }
        return ServerResponseEntity.success(authSocialVO);
    }

}
